package com.atguigu.gulimall.product.thread;

import java.util.concurrent.*;

public class ThreadTest {
    public static void main(String[] args) {
        //通过Executors工具类创建线程池
        ExecutorService executorService = Executors.newFixedThreadPool(10);
        //原生方法创建线程池
        //ThreadPoolExecutor threadPoolExecutor = new ThreadPoolExecutor();
        System.out.println("main.....开始。。。");
//        CompletableFuture.runAsync(() ->{
//            System.out.println("当前线程是："+Thread.currentThread().getName());
//            int i=10/2;
//            System.out.println("运行结果是："+i);
//        },executorService);
//        CompletableFuture<Integer> future = CompletableFuture.supplyAsync(() -> {
//            System.out.println("当前线程是：" + Thread.currentThread().getName());
//            int i = 10 / 0;
//            System.out.println("运行结果是：" + i);
//            return i;
//        }, executorService).whenComplete((res,exception)->{
//            System.out.println("结果是："+res+";异常是："+exception);
//        }).exceptionally(throwable -> {
//            return 10;
//        });
//        Integer integer=null;
//        try {
//             integer = future.get();
//        } catch (InterruptedException e) {
//            e.printStackTrace();
//        } catch (ExecutionException e) {
//            e.printStackTrace();
//        }
        //使用线程池中的线程去执行任务
//        CompletableFuture<Integer> future01 = CompletableFuture.supplyAsync(() -> {
//            System.out.println("当前线程是：" + Thread.currentThread().getName());
//            int i = 10 / 2;
//            System.out.println("运行结果是：" + i);
//            return i;
//        }, executorService);// thenRunAsync(() -> {
////            System.out.println("任务2开始执行了。。。。");
////        }, executorService);
//        CompletableFuture<String> future02 = CompletableFuture.supplyAsync(() -> {
//            return "Hello";
//        }, executorService);
//        CompletableFuture<Void> voidCompletableFuture = future01.thenAcceptBothAsync(future02, (f1, f2) -> {
//            System.out.println("任务3开始。。。" + f1 + "--->" + f2);
//        }, executorService);
//        future01.runAfterEitherAsync(future02,()->{
////            System.out.println("任务三开始执行。。。。");
////        },executorService);
//        CompletableFuture<String> futureImg = CompletableFuture.supplyAsync(() -> {
//            System.out.println("查询图片信息。。");
//            return "hello.jpg";
//        }, executorService);
//        CompletableFuture<String> futureAttr = CompletableFuture.supplyAsync(() -> {
//            System.out.println("查询属性信息。。。");
//            return "红色+256G";
//        }, executorService);
//        CompletableFuture<String> futureDesc = CompletableFuture.supplyAsync(() -> {
//            System.out.println("查询产品描述。。。。");
//            return "华为手机";
//        }, executorService);
//        CompletableFuture<Void> future = CompletableFuture.allOf(futureImg, futureAttr, futureDesc);
//        try {
//            future.get();    //阻塞式等待 ，等待三个任务都执行完成后，再向下指执行
//        } catch (InterruptedException e) {
//            e.printStackTrace();
//        } catch (ExecutionException e) {
//            e.printStackTrace();
//        }
        CompletableFuture<Integer> future01 = CompletableFuture.supplyAsync(() -> {
            System.out.println("当前线程是：" + Thread.currentThread().getName());
            int i = 10 / 2;
            System.out.println("运行结果是：" + i);
            return i;
        }, executorService);
        future01.thenAcceptAsync((f)->{
            System.out.println("haha"+f);
        },executorService);
        System.out.println("main....结束。。。");

    }
}
